import '../vendor/cors/jquery-ajax-transport-xdomainrequest'
import toastr from '../vendor/toastr/toastr'

import {cfg} from '../config/config.js'
import {router} from '../config/router.js'

var tip = (function() {
    toastr.options = {
        'closeButton': true,
        'debug': false,
        'newestOnTop': true,
        'progressBar': true,
        'positionClass': 'toast-top-center',
        'preventDuplicates': false,
        'onclick': null,
        'showDuration': '300',
        'hideDuration': '1000',
        'timeOut': '3000',
        'extendedTimeOut': '1000',
        'showEasing': 'swing',
        'hideEasing': 'linear',
        'showMethod': 'fadeIn',
        'hideMethod': 'fadeOut'
    };

    return {
        info:toastr.info,
        succ:toastr.success,
        warn:toastr.warning,
        err:toastr.error
    }
})();

var ajax = (function(){
    function send(url,ajaxName){
        return function(arg){
            var fullurl = url;
            var type = arg.type?arg.type:'get';

            var opt = {
                type: type,
                dataType: 'json',
                xhrFields: {
                    withCredentials: true
                }
            }

            if(Object.prototype.toString.call(arg.param)=='[object FormData]'){
                opt.data = arg.param;
                opt.cache = false;
                opt.processData = false;
                opt.contentType = false;
                opt.type = 'post';
            } else {
                if(type=='post'){
                    opt.data = arg.param?arg.param:{};
                } else {
                    var par_arr = [];
                    for (const key in arg.param) {
                        if (arg.param.hasOwnProperty(key)) {
                            const val = arg.param[key];
                            par_arr.push(key + '=' + val);
                        }
                    }
                    fullurl = par_arr.length>0?(fullurl+'?'+par_arr.join('&')):fullurl;
                }
            }

            opt.url = fullurl;

            $.ajax(opt).done(function(data){
                if(data.rcode==cfg.rcode.SUCC||data.rcode==cfg.rcode.NOREQ){
                    arg.cb(data);
                } else if(data.rcode==cfg.rcode.NOLOGIN){
                    if(arg.loginFailCb)arg.loginFailCb();
                } else {
                    tip.err(data.message);
                    if(arg.failCb)arg.failCb(data);
                }
            }).fail(function(){
                if(arg.failCb)arg.failCb();
            });
        };
    }

    return {
        login:send(cfg.serviceUrl+'/admin/login','登录'),
        uploadImg:send(cfg.serviceUrl+'/uploadImg','图片上传'),

        typeList:send(cfg.serviceUrl+'/admin/goods/type/list','获取类型列表'),
        goodsList:send(cfg.serviceUrl+'/admin/goods/list','获取商品列表'),
        goodsSave:send(cfg.serviceUrl+'/admin/goods/save','添加商品'),
        goodsEdit:send(cfg.serviceUrl+'/admin/goods/save','编辑商品'),
        goodsDel:send(cfg.serviceUrl+'/admin/goods/del','删除商品'),
        goodsInfo:send(cfg.serviceUrl+'/admin/goods/info','查看商品'),

        promotionsList:send(cfg.serviceUrl+'/promotion/findPromotions','活动列表'),
        promotionsSave:send(cfg.serviceUrl+'/admin/promotion/save','活动列表'),

        getOrderList:send(cfg.serviceUrl+'/admin/shopping/order/list','订单列表'),
        updateOrderState:send(cfg.serviceUrl+'/admin/shopping/order/updateState','修订单状态'),
        // 获取前台用户token par:phoneNum
        getTokenByPhone:send(cfg.serviceUrl+'/getTokenByPhone','获取前台用户token'),
        // 前台order调用 需使用前台token
        fCreateOrder: send(cfg.serviceUrl + '/front/shopping/order/create', '创建订单'),
        fUpdateOrder: send(cfg.serviceUrl + '/front/shopping/order/update', '更新订单'),
        fOrderPayComplete:send(cfg.serviceUrl+'/front/shopping/order/pay','订单支付完成'),

        getUserInfo:send(cfg.serviceUrl+'/admin/user/getUserInfo'),
        getUserList:send(cfg.serviceUrl+'/admin/user/list'),
        addUserInfo:send(cfg.serviceUrl+'/admin/user/saveUserInfo'),
        editUserInfo:send(cfg.serviceUrl+'/admin/user/saveUserInfo'),


        
    };
})();

export {
    tip,ajax
}